package com.company.hrmadmin.modules.event;

import com.alibaba.fastjson.JSON;
import com.company.hrmadmin.modules.entity.dto.kafka.Event;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * 事件生产者
 *
 * @author Song, Wei
 * Date: 2022/4/22 17:31
 */
@Slf4j
@Component
public class EventProducer {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 处理事件，将 Event 对象转换成 JSON 字符串，发送到 Kafka 队列中
     *
     * @param event event 事件对象
     */
    public void sendEvent(Event event) {
        kafkaTemplate.send(event.getTopic(), JSON.toJSONString(event));
    }

}
